// pages/collection/index.js
const http = require('../../utils/http')
const config = require('../../utils/config')
Page({
  data: {
    userId: "",
    collectionList: [],
    isCollection: false,
    collectionId: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("收藏页面")
    var userId = wx.getStorageSync('userId');
    this.setData({
      userId: userId
    })
    this.getCollectionDetail()
  },
  getCollectionDetail() {
    var page = this
    // var params = {
    //   url:config.collectUrl,
    var params = {
      url: config.collectUrl,
      method: "GET",
      data: {
        userId: this.data.userId
      },
      callBack(res) {
        console.log(res)
        // console.log("res:",  res[0].records)
        page.setData({
          collectionList: res,
          isCollection: true,
          collectionId: res[0].id,
        })
      },

      errCallBack(res) {}
    }
    http.request(params)
  },
  gotoDetail(e) {
    console.log(e);
    wx.navigateTo({
      url: '../detail/detail?prodId=' + e.currentTarget.dataset.prodid,
    })
  },

  /***
   * 更新商品的收藏状态
   * 添加或取消商品的收藏
   */
  cancleCollection(e) {
    var collectionId = e.currentTarget.dataset.collection
    var page = this
    console.log("取消收藏", collectionId)
    var params = {
      url: '/collect?id=' + collectionId,
      method: "DELETE",
      callBack(res) {
        page.setData({
          isCollection: !page.data.isCollection
        })
        page.getCollectionDetail()
        wx.hideLoading()
      },
      errCallBack(res) {}
    }
    console.log(params);
    http.request(params)
  }
})